﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GeradorMassaDados.Bovespa.Business
{
    public class DataCollectorBiz
    {
        #region Properties

        private IDataAcess DataAcess { get; set; }

        #endregion

        #region Constructor

        public DataCollectorBiz()
        {
            DataAcess = new DataAcess();
        }

        #endregion

        #region Methods

        public string[] GetUserNames()
        {
#if DEBUG
            return DataAcess.GetUserNames();
#else
            return new string[] { System.Environment.MachineName.Trim() };
#endif
        }

        public List<SQLCommandLog> GetCommandLogs(CollectItem item)
        {
            item.Test();
            var logs = DataAcess.GetCommandLogs(item);
            return logs;
        }

        public void Save(List<SQLCommandLog> commandLogs, string path)
        {
            if (File.Exists(path)) File.Delete(path);

            using (var writeFile = new StreamWriter(path, false, Encoding.UTF8))
            {
                foreach (var item in commandLogs)
                {
                    writeFile.Write(item.SQLFormated);
                    if (!item.SQLFormated.EndsWith(";"))
                        writeFile.Write(";");
                    writeFile.WriteLine();
                }
                writeFile.Flush();
                writeFile.Close();
            }
        }

        #endregion
    }
}